<?php

/**
 * list actions.
 *
 * @package    estate
 * @subpackage list
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class listActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
   if ( $request->hasParameter( 'clear' ) )
   {
    $this->getUser()->getAttributeHolder()->removeNamespace( 'booking' );
    $this->getUser()->getAttributeHolder()->removeNamespace( 'paypal' );
   }
   
    $apartman_criteria = $this->getUser()->getAttributeHolder()->getAll('booking' );
    
    $c = new Criteria();    
    
    // make query for bookings to get the apartmens to be excluded
    if ( $apartman_criteria )
    {
     $end = mktime( 23, 59, 59, $apartman_criteria['date_depart']['month'], $apartman_criteria['date_depart']['day'], $apartman_criteria['date_depart']['year'] );
     $start = mktime( 0, 1, 1, $apartman_criteria['date_arrive']['month'], $apartman_criteria['date_arrive']['day'], $apartman_criteria['date_arrive']['year'] );
     
     
     $cs = new Criteria();
     $cs->add( ApartmanBookingPeer::CHECK_IN, $end , Criteria::LESS_THAN );
     $cs->addOr( ApartmanBookingPeer::CHECK_OUT, $start , Criteria::GREATER_THAN );
     $bookings = ApartmanBookingPeer::doSelect( $cs );
     
     $excludes = array();
     foreach( $bookings as $booking )
     {
      $excludes[] = $booking->getApartmanId();
     }
     
     $c->add( ApartmanPeer::MAX_OCCUPANCY, $apartman_criteria['adults'] + $apartman_criteria['children'], Criteria::GREATER_EQUAL );
     $c->add( ApartmanPeer::ID, $excludes, Criteria::NOT_IN );
    }
    
    $this->pager = new sfPropelPager( 'Apartman', 20 );
    $this->pager->setPage( $this->getRequestParameter('page', 1) );
    $this->pager->setCriteria( $c );
    $this->pager->init();
  }
}
